/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.upn.sigemfi.rec.beans;

import edu.upn.sigemfi.rec.bc.CarteraDelegate;
import edu.upn.sigemfi.rec.be.Cartera;
import java.util.ArrayList;
import java.util.List;
import javax.faces.model.SelectItem;

/**
 *
 * @author Diego
 */
public class CarteraController {

    private CarteraDelegate delegado;
    private Cartera cartera;
    private List<SelectItem> Carteras;
    private String selectedItem;

    public Cartera getCartera() {
        return cartera;
    }

    public void setCartera(Cartera cartera) {
        this.cartera = cartera;
    }

    public CarteraController() {
        delegado = new CarteraDelegate();
        cartera = new Cartera();
    }

    public List<Cartera> getCarteras() throws Exception {
        try {
            return delegado.listar();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public List<SelectItem> getListaCarteras() {
        List<SelectItem> combo = null;
        try {

            List<Cartera> lista = delegado.listar();
            combo = new ArrayList<SelectItem>();
            if (lista.size() > 0) {
                combo.add(new SelectItem(String.valueOf(0), "Seleccionar Cartera"));
                for (Cartera t : lista) {
                    combo.add(new SelectItem(String.valueOf(t.getIdCartera()), t.getNombre()));
                }
            } else {
                combo.add(new SelectItem(String.valueOf(0), "No hay Registros"));
            }

        } catch (Exception e) {
            combo.add(new SelectItem(String.valueOf(0), "ERROR"));
        }

        return combo;
    }

    public String nuevo() {
        cartera = new Cartera();
        return "nuevo";
    }

    public String registrar() {
        try {
            delegado.crear(cartera);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "volver";
    }

    public String actualizar() {
        try {
            delegado.editar(cartera);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "volver";
    }

    public String eliminar() {
        try {
            delegado.eliminar(cartera);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "volver";
    }
}
